using GLib;

/**
 * Logger
 * 	- prefix delegate (customizaton);
 *	- output format [SomeType]YouPrefix:Message
 *
 */
namespace Slurp.Logger { 

	delegate string DelegatePrefix(string type);
	
	class Logger:Object {

		protected DataOutputStream stream;

		public DelegatePrefix prefix_delegate;


		public Logger(string filepath) throws Error {
			File file = File.new_for_path(filepath);			
			
			if(!file.query_exists()) {
				FileOutputStream stream = file.create(FileCreateFlags.PRIVATE);
				this.stream = new DataOutputStream(stream);
			} else {
				FileOutputStream stream = file.append_to(FileCreateFlags.PRIVATE);
				this.stream = new DataOutputStream(stream);
			}
			this.prefix_delegate = this.prefix;
			return;
		}

		protected string prefix(string type) {
			string username = Environment.get_user_name();
			DateTime date_time = new DateTime.now_local();
			return "["+type+"]["+date_time.to_string()+"]["+username+"]: ";			
		}

		protected void log(string type, string message) {
			try {
				string prefix = this.prefix_delegate(type);
				this.stream.put_string(prefix+message+"\n");
			} catch(Error e) {
				print(e.message);
			}
		}

		public void warning(string warning) {
			this.log("WARNING", warning);
		}

		public void info(string info) {
			this.log("INFO", info);
		}

		public void error(string error) {
			this.log("ERROR", error);
		}
	
	}
}
